home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Expanders / Function / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  4.9 KB  |  189 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "DEV_IE:Expanders/defs.h"
  15.  
  16. extern __geta4 struct Library *LibInit   ( __A0 BPTR );
  17. extern __geta4 struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  18. extern __geta4 long            LibClose  ( __A0 struct Library * );
  19. extern __geta4 long            LibExpunge( __A0 struct Library * );
  20.  
  21. struct Expander *LibBase = NULL;
  22.  
  23. long SysBase        = NULL;
  24. long DOSBase        = NULL;
  25. long IntuitionBase  = NULL;
  26. long ReqToolsBase   = NULL;
  27. BPTR SegList        = 0;
  28. UBYTE *Desc         = NULL;
  29.  
  30. /*
  31.  *    The Initialization routine is given only a seglist pointer.  Since
  32.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  33.  *    and return either NULL or the library pointer.  Exec has Forbid()
  34.  *    for us during the call.
  35.  */
  36.  
  37. struct Library *LibInit( __A0 BPTR segment )
  38. {
  39.  
  40.     struct Library *lib = NULL;
  41.  
  42.     static const long Vectors[] = {
  43.  
  44.     (long)ALibOpen,         /*  standard lib functions    */
  45.     (long)ALibClose,
  46.     (long)ALibExpunge,
  47.     (long)ALibReserved,
  48.  
  49.     (long)IEX_Mount,        /*  mount function            */
  50.  
  51.     (long)IEX_Add,          /*  edit functions            */
  52.     (long)IEX_Remove,
  53.     (long)IEX_Edit,
  54.     (long)IEX_Copy,
  55.     (long)IEX_Make,
  56.     (long)IEX_Free,
  57.     (long)IEX_Refresh,
  58.  
  59.     (long)IEX_Save,         /*  I/O functions             */
  60.     (long)IEX_Load,
  61.  
  62.     (long)IEX_StartSrcGen,  /*  source related functions  */
  63.     (long)IEX_WriteGlobals, 
  64.     (long)IEX_WriteSetup,
  65.     (long)IEX_WriteCloseDown,
  66.     (long)IEX_WriteHeaders,
  67.     (long)IEX_WriteRender,
  68.     (long)IEX_GetIDCMP,
  69.     (long)IEX_WriteData,
  70.     (long)IEX_WriteChipData,
  71.     (long)IEX_WriteOpenWnd,
  72.     (long)IEX_WriteCloseWnd,
  73.     -1
  74.     };
  75.  
  76.     SysBase = *(long *)4;
  77.  
  78.     if( DOSBase = OpenLibrary( "dos.library", 36 )) {
  79.     if( IntuitionBase = OpenLibrary( "intuition.library", 36 )) {
  80.         if( ReqToolsBase = OpenLibrary( "reqtools.library", 38 )) {
  81.  
  82.         if( LibBase = lib = MakeLibrary( (APTR)Vectors, NULL, NULL, sizeof( struct Expander ), NULL )) {
  83.  
  84.             lib->lib_Node.ln_Type = NT_LIBRARY;
  85.             lib->lib_Node.ln_Name = LibName;
  86.             lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  87.             lib->lib_Version      = 37;
  88.             lib->lib_Revision     = 0;
  89.             lib->lib_IdString     = (APTR)LibId;
  90.  
  91.             SegList = segment;
  92.  
  93.             AddLibrary( lib );
  94.         }
  95.  
  96.         }
  97.     }
  98.     }
  99.  
  100.     return( lib );
  101. }
  102.  
  103. /*
  104.  *    Open is given the library pointer and the version request.  Either
  105.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  106.  *    Exec has Forbid() for us during the call.
  107.  */
  108.  
  109. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  110. {
  111.     ++lib->lib_OpenCnt;
  112.  
  113.     lib->lib_Flags &= ~LIBF_DELEXP;
  114.  
  115.     return( lib );
  116. }
  117.  
  118. /*
  119.  *    Close is given the library pointer and the version request.  Be sure
  120.  *    not to decrement the open count if already zero.  If the open count
  121.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  122.  *    and return the seglist.  Otherwise we return NULL.
  123.  *
  124.  *    Note that this routine never sets LIBF_DELEXP on its own.
  125.  *
  126.  *    Exec has Forbid() for us during the call.
  127.  */
  128.  
  129. long LibClose( __A0 struct Library *lib )
  130. {
  131.     if( lib->lib_OpenCnt && --lib->lib_OpenCnt )
  132.     return( NULL );
  133.  
  134.     if( lib->lib_Flags & LIBF_DELEXP )
  135.     return( LibExpunge( lib ));
  136.  
  137.     return( NULL );
  138. }
  139.  
  140. /*
  141.  *    We expunge the library and return the Seglist ONLY if the open count
  142.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  143.  *    flag and return NULL.
  144.  *
  145.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  146.  *    might be called from the memory allocator and thus we CANNOT DO A
  147.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  148.  *
  149.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  150.  *    therefore freeze if we called it ourselves.  As far as I can tell
  151.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  152.  *    below.
  153.  */
  154.  
  155. long LibExpunge( __A0 struct Library *lib )
  156. {
  157.     if( lib->lib_OpenCnt ) {
  158.  
  159.     lib->lib_Flags |= LIBF_DELEXP;
  160.     return( NULL );
  161.     }
  162.  
  163.     Remove( &lib->lib_Node );
  164.  
  165.     FreeMem(( char * )lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize );
  166.  
  167.     if( DOSBase ) {
  168.     CloseLibrary( (struct Library *)DOSBase );
  169.     DOSBase = NULL;
  170.     }
  171.  
  172.     if( IntuitionBase ) {
  173.     CloseLibrary( (struct Library *)IntuitionBase );
  174.     IntuitionBase = NULL;
  175.     }
  176.  
  177.     if( ReqToolsBase ) {
  178.     CloseLibrary( (struct Library *)ReqToolsBase );
  179.     ReqToolsBase = NULL;
  180.     }
  181.  
  182.     if( Desc ) {          /* free the descriptions file  */
  183.     FreeVec( Desc );  /* loaded by IEX_Mount         */
  184.     Desc = NULL;
  185.     }
  186.  
  187.     return(( long )SegList );
  188. }
  189.